cc.Class({
  extends: cc.Component,

  properties: {
    gamePlayer: {
      default: null,
      type: cc.Prefab
    },
    gamePlayArr: {
      default: [],
      type: cc.Prefab
    },
    pageView: {
      default: null,
      type: cc.Node
    },
    content: {
      default: null,
      type: cc.Node
    },
    sprite: {
      default: null,
      type: cc.SpriteFrame
    },
    myRank: {
      default: null,
      type: cc.Node
    },
  },

  start: function() {
    this.nickName = null;
    wx.getUserInfo({
      openIdList: ['selfOpenId'],
      lang: 'zh_CN',
      success: res => {
        this.nickName = res.data[0].nickName;
        console.log(res.data, res.data[0].nickName, this.nickName, '获取自身信息');
      }
    });
    wx.getFriendCloudStorage({
      keyList: ['maxScore'], // 你要获取的、托管在微信后台的key
      success: res => {
        this.obData = res.data;
        console.log(this.obData, '获取朋友信息');
        this.obData.sort(this.sortNumber('KVDataList'));
        // 开始分配数据
        this.srotingRank();
        // 计算自己名次
        this.getMaxScore();
        console.log(this.obData, this.obData.length, '获取朋友信息');
      }
    });
  },
  // 分页排行
  srotingRank: function() {
    var pageview = this.pageView.getComponent(cc.PageView);
    if (this.obData.length <= 10) { // 0-9
      this.content.width = 900;
      this.addPageLankAB(this.obData.length, pageview);
    } else if (this.obData.length > 10 && this.obData.length <= 20) { // 10-19
      this.content.width = 1800;
      var num1 = Math.floor * ((this.obData.length - 1) / 10) * 10; // 得出的都是 10
      var num2 = (this.obData.length - 1) % 10; // 得出的是 10-19
      this.addPageLankAB(num1, pageview); // 创建0-9
      this.addPageLankAB(num2, pageview); // 创建10-19
    } else if (this.obData.length > 20 && obData.length <= 30) { // 20-29
      this.content.width = 2700;
      var num1 = Math.floor * ((this.obData.length - 1) / 10) * 10; // 得出的都是 20
      var num2 = (this.obData.length - 1) % 10; // 得出的是 20-29
      this.addPageLankAB(10, pageview); // 创建0-9
      this.addPageLankAB(num1, pageview); // 创建10-19
      this.addPageLankAB(num2, pageview); // 创建20-29
    } else if (this.obData.length > 30 && this.obData.length <= 40) { // 30-39
      this.content.width = 3600;
      var num1 = Math.floor * ((this.obData.length - 1) / 10) * 10; // 得出的都是 30
      var num2 = (this.obData.length - 1) % 10; // 得出的是 31-39
      this.addPageLankAB(10, pageview); // 创建0-9
      this.addPageLankAB(20, pageview); // 创建10-19
      this.addPageLankAB(num1, pageview); // 创建20-29
      this.addPageLankAB(num2, pageview); // 创建30-39
    } else if (this.obData.length > 40 && this.obData.length <= 50) { // 40-49
      this.content.width = 4500;
      var num1 = Math.floor * ((this.obData.length - 1) / 10) * 10; // 得出的是 40
      var num2 = (this.obData.length - 1) % 10; // 得出的是 41-49
      this.addPageLankAB(10, pageview); // 创建0-9
      this.addPageLankAB(20, pageview); // 创建10-19
      this.addPageLankAB(30, pageview); // 创建20-29
      this.addPageLankAB(num1, pageview); // 创建30-39
      this.addPageLankAB(num2, pageview); // 创建40-49
    } else {
      this.content.width = 4500;
      this.addPageLankAB(10, pageview); // 创建0-9
      this.addPageLankAB(20, pageview); // 创建10-19
      this.addPageLankAB(30, pageview); // 创建20-29
      this.addPageLankAB(40, pageview); // 创建30-39
      this.addPageLankAB(9, pageview); // 创建40-49
    }
  },
  addPageLankAB: function(num, pageview) {
    var page = new cc.Node('new_page');
    var node = cc.instantiate(page);
    pageview.addPage(node);
    node.x = 450;
    var sp = node.addComponent(cc.Sprite);
    sp.spriteFrame = this.sprite;
    node.width = 900;
    node.height = 400;
    var ly = node.addComponent(cc.Layout);
    ly.type = 3;
    ly.spacingX = 4;
    ly.spacingY = 3;
    ly.verticalDirection = 1;
    ly.horizontalDirection = 0;
    if (num <= 10) {
      for (var i = 0; i < num; i++) {
        let playerDan = cc.instantiate(this.gamePlayer);
        node.addChild(playerDan);
        this.createImage(playerDan.getChildByName('headMask').getChildByName('head').getComponent(cc.Sprite), this.obData[i].avatarUrl);
        this.createName(playerDan.getChildByName('PlayerName').getComponent(cc.Label), this.obData[i].nickname);
        this.createScore(playerDan.getChildByName('Score').getComponent(cc.Label), this.obData[i].KVDataList);
        this.createRank(playerDan.getChildByName('Rank').getComponent(cc.Label), i);
      }
    } else if (num <= 20) {
      for (var i = 10; i < num; i++) {
        let playerDan = cc.instantiate(this.gamePlayer);
        node.addChild(playerDan);
        this.createImage(playerDan.getChildByName('headMask').getChildByName('head').getComponent(cc.Sprite), this.obData[i].avatarUrl);
        this.createName(playerDan.getChildByName('PlayerName').getComponent(cc.Label), this.obData[i].nickname);
        this.createScore(playerDan.getChildByName('Score').getComponent(cc.Label), this.obData[i].KVDataList);
        this.createRank(playerDan.getChildByName('Rank').getComponent(cc.Label), i);
      }
    } else if (num <= 30) {
      for (var i = 20; i < num; i++) {
        let playerDan = cc.instantiate(this.gamePlayer);
        node.addChild(playerDan);
        this.createImage(playerDan.getChildByName('headMask').getChildByName('head').getComponent(cc.Sprite), this.obData[i].avatarUrl);
        this.createName(playerDan.getChildByName('PlayerName').getComponent(cc.Label), this.obData[i].nickname);
        this.createScore(playerDan.getChildByName('Score').getComponent(cc.Label), this.obData[i].KVDataList);
        this.createRank(playerDan.getChildByName('Rank').getComponent(cc.Label), i);
      }
    } else if (num <= 40) {
      for (var i = 30; i < num; i++) {
        let playerDan = cc.instantiate(this.gamePlayer);
        node.addChild(playerDan);
        this.createImage(playerDan.getChildByName('headMask').getChildByName('head').getComponent(cc.Sprite), this.obData[i].avatarUrl);
        this.createName(playerDan.getChildByName('PlayerName').getComponent(cc.Label), this.obData[i].nickname);
        this.createScore(playerDan.getChildByName('Score').getComponent(cc.Label), this.obData[i].KVDataList);
        this.createRank(playerDan.getChildByName('Rank').getComponent(cc.Label), i);
      }
    } else if (num <= 50) {
      for (var i = 40; i < num; i++) {
        let playerDan = cc.instantiate(this.gamePlayer);
        node.addChild(playerDan);
        this.createImage(playerDan.getChildByName('headMask').getChildByName('head').getComponent(cc.Sprite), this.obData[i].avatarUrl);
        this.createName(playerDan.getChildByName('PlayerName').getComponent(cc.Label), this.obData[i].nickname);
        this.createScore(playerDan.getChildByName('Score').getComponent(cc.Label), this.obData[i].KVDataList);
        this.createRank(playerDan.getChildByName('Rank').getComponent(cc.Label), i);
      }
    }
  },
  // 重新排序
  sortNumber: function(property) {
    return function(a, b) {
      for (var i = 0; i < a[property].length; i++) {
        if (a[property][i].key == 'maxScore') {
          var value1 = a[property][i].value;
        }
      }
      for (var j = 0; j < b[property].length; j++) {
        if (b[property][j].key == 'maxScore') {
          var value2 = b[property][j].value;
        }
      }
      return value2 - value1;
    }
  },
  // 自身获取分数
  getMaxScore: function() {
    wx.getUserCloudStorage({
      keyList: ['maxScore'],
      success: res => {
        let usrData = res.KVDataList;
        console.log(res, res.KVDataList, '分数数据222');
        if (usrData.length > 0) {
          console.log('不是第一次跑');
          for (var i = 0; i < this.obData.length; i++) {
            if (this.obData[i].nickname == this.nickName) {
              this.myRank.getComponent(cc.Label).string = '第' + (i + 1) + '名';
            }
          }
        } else {
          this.myRank.getComponent(cc.Label).string = '未上榜';
        }
      }
    })
  },
  // 头像图片
  createImage: function(sprite, url) {
    let image = wx.createImage();
    image.onload = function() {
      let texture = new cc.Texture2D();
      texture.initWithElement(image);
      texture.handleLoadedTexture();
      sprite.spriteFrame = new cc.SpriteFrame(texture);
    };
    image.src = url;
  },
  // 昵称
  createName: function(label, nickName) {
    label.string = nickName;
  },
  // 分数
  createScore: function(label, arr) {
    label.string = arr[0].value + ' 分';
  },
  // 排名
  createRank: function(label, num) {
    label.string = num + 1;
  }
});